// pages/personal/personal.js
var app = getApp();
var util = require('../../utils/util');
Page({

    /**
     * 页面的初始数据
     */
    data: {
		currentPage: 1,
		startdate:"",
		enddate:"",
		orderlist: [],
    },
	/**
		* 生命周期函数--监听页面显示
		*/
	onShow: function () {
		var that = this;
		var data = new Date;
		that.setData({
			currentPage: 1,
			orderlist: [],
		})
		var month = (data.getMonth() + 1);
		console.log(data.getFullYear(), (data.getMonth() + 1), data.getDate())
		month = month < 10 ? '0' + month : month
		var day = new Date(data.getFullYear(), month, 0) //获取当月的最后一天
		//	console.log(day.getDate())

		that.setData({
			startdate: data.getFullYear() + "-" + (data.getMonth() + 1) + "-" + 1,
			enddate: data.getFullYear() + "-" + (data.getMonth() + 1) + "-" + day.getDate()
		})
		app.checkLogin(function () {
			that.setData({
				date: util.formatTime(new Date)
			})
			that.getStoreInfo();
			that.myOrderList();
		})
	},
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
		var that = this;
		util.fetchPost("/api/v1/common/system", {}, function (res) {
			that.setData({
				kefu: res.datas.kefu_phone,
			})
		})
    },
    //选择日期
	startDateChange: function (e) {
		var that = this;
		console.log('picker发送选择改变，携带值为', e.detail.value)
		this.setData({
			startdate: e.detail.value,
			currentPage: 1,
			orderlist: [],
		})
		that.myOrderList();
	},

	endDateChange: function (e) {
		var that = this;
		console.log('picker发送选择改变，携带值为', e.detail.value)
		this.setData({
			enddate: e.detail.value,
			currentPage: 1,
			orderlist: [],
		})
		that.myOrderList();
	},
    //结算记录
    onDetailedTap() {
        wx.navigateTo({
            url: '../detailed/detailed'
        })
    },
	//结算须知
	onEttlementTap(){
		wx.navigateTo({
			url: '../settlement/settlement',
		})
	},
	getStoreInfo: function () {
		var that = this;
		util.showLoading();
		var apiUrl = '/api/dis/member/index';
		var apiData = {
		}
		util.fetchPost(apiUrl, apiData, function (res) {
			util.hideLoading();
			if (res.state == 1) {

				that.setData({
					store_info: res.datas
				});
			} else {
				util.message(res.datas);
			}
		});
	},
	//我的订单
	myOrderList: function () {
		var that = this;
		util.showLoading();
		var apiUrl = '/api/dis/order/order_total_for_mine';
		var apiData = {
			s_time: that.data.startdate,
			e_time: that.data.enddate,
			page: that.data.currentPage
		}
		util.fetchPost(apiUrl, apiData, function (res) {
			util.hideLoading();
			if (res.state == 1) {
				var allpost = that.data.orderlist;
				if (allpost == undefined) {
					allpost = [];
				}
				for (var i in res.datas.list) {
					allpost.push(res.datas.list[i])
				}
				if (res.datas.length < 10) {
					that.setData({
						tishi_title: '已加载全部',
					});
				} else {
					that.setData({
						tishi_title: '加载更多',
					});
				}
				that.setData({
					orderlist: allpost,
					orderinfo: res.datas,
					ready: res.datas.length == 0 ? 1 : 2,
				});
				console.log(res.datas);
			} else {
				util.message(res.datas);
			}
		});
	},
	//退出登录
	closeLoginTap:function(e){
		wx.removeStorageSync("dis_id");
		wx.removeStorage({
			key: 'pwdInfo',
			success: function(res) {
				wx.reLaunch({
					url: '../login/login',
				})
			},
		})
	},
	//客服电话
	onPhoneTap: function (e) {
		var that = this;
		wx.makePhoneCall({
			phoneNumber: that.data.kefu //仅为示例，并非真实的电话号码
		})
	},
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

	/**
   * 页面相关事件处理函数--监听用户下拉动作
   */
	onPullDownRefresh: function () {
		var that = this;
		that.setData({
			orderinfo: [],
			currentPage: 1
		})
		that.myOrderList()
	},
	/**
	* 页面上拉触底事件的处理函数
	*/
	onReachBottom: function () {
		var that = this;
		if (that.data.ready == 2) {
			var page = that.data.currentPage + 1
			that.setData({
				currentPage: page,
			})
			that.myOrderList()
		}
	},
    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})